<template>
	<view class="recharge">
		<!-- 优惠充值 -->
		<u-navbar leftIconSize="34rpx" autoBack :placeholder="true" bgColor="#ffffff">
			<view class="" slot="center">
				<text class="c-000 pf-sc fw-600" style="font-size: 34rpx;">优惠充值</text>
			</view>
		</u-navbar>
		<view
			class="set-data-container w-750 b-box d-flex flex-c ali-i-c  bg-ff po-re"
			style="padding:0 30rpx;"
			:style="{ height: containerHeight }"
		>
			<!-- avatar -->
			<view
				style="height: 208rpx;padding: 0 34rpx;"
				class="d-flex flex-r ali-i-c just-s w-100 b-box"
			>
				<view
					style="width: 104rpx;height: 104rpx;border-radius: 120rpx;overflow: hidden;margin-right: 18rpx;"
				>
					<image
						:src="'https://www.chejingjing.cn' + user.avatar"
						style="width: 104rpx;height: 104rpx;"
					></image>
				</view>
				<view style="height: 104rpx;" class="d-flex flex-c ali-i-s just-sw b-box">
					<text class="pf-sc fw-500 c-333" style="font-size: 34rpx;">{{ user.nickname }}</text>
					<text class="pf-sc fw-500" style="color: #262626;font-size: 34rpx;">
						￥{{ user.money }}
					</text>
				</view>
			</view>
			<!-- 三个选项 1-->
			<view
				class="w-100 b-box d-flex flex-r ali-i-c just-sw"
				v-for="(item, index) in list"
				@click="SelectItem(index, item.id)"
				:class="{ activecolor: CurrentSelect == index }"
				style="border: 2rpx solid #DDDDDD;height: 116rpx;border-radius: 6rpx;padding: 0 40rpx;margin-bottom: 28rpx;"
			>
				<view class="d-flex flex-r ali-i-c just-s">
					<text class="fw-500 c-333 pf-sc fz-32">充{{ item.paymoney }}</text>
					<text class="fw-500 pf-sc fz-32 po-re" style="color:#FF5C00;top:-2rpx">
						（送{{ item.sendmoney }}）
					</text>
				</view>
				<view>
					<text class="fw-400 pf-sc fz-30 po-re" style="color:#FF5C00;">¥{{ item.paymoney }}</text>
				</view>
			</view>
			<!-- 三个选项 3-->
			<!-- 充值按钮 -->
			<view style="margin-top: 52rpx;" class="w-100 d-flex flex-r ali-i-c just-c b-box">
				<u-button
					:throttleTime="700"
					@click="Recharge"
					:customStyle="{
						height: '88rpx',
						width: '670rpx',
						font: '500 32rpx PingFang SC',
						color: '#ffffff',
						borderWidth: '0rpx',
						background: 'linear-gradient(180deg, #4885C5 0%, #3B52A6 100%)',
						borderRadius: '44rpx'
					}"
					shape="square"
					text="充值"
				></u-button>
			</view>
		</view>
	</view>
</template>

<script>
import { rechargelist, chargemoney } from '@/common/api.js';
import { _containerHeight } from '@/utils/GetSys.js';
import Unavbar from '../components/uview-ui/components/u-navbar/u-navbar';
import Ubutton from '../components/uview-ui/components/u-button/u-button';
export default {
	components: {
		'u-navbar': Unavbar,
		'u-button': Ubutton
	},
	data() {
		return {
			containerHeight: '',
			CurrentSelect: 0, //当前选中套餐
			user: uni.getStorageSync('userInfo'),
			list: [],
			id: '' //充值卡的id
		};
	},
	methods: {
		SelectItem(index, id) {
			//当前选中充值套餐
			console.log(id);
			this.id = id;
			this.CurrentSelect = index;
		},
		Recharge() {
			//充值
			if (this.id == '') {
				this.id = this.list[0].id;
			}
			let params = {
				token: uni.getStorageSync('usertoken'),
				id: this.id
			};
			console.log(this.id);

			chargemoney(params).then(res => {
				if (res.data.code == 1) {
					var paydata = res.data.data;
					uni.requestPayment({
						provider: 'wxpay',
						// "orderInfo": {  },
						// "appid":paydata.appId,  // 微信开放平台 - 应用 - AppId，注意和微信小程序、公众号 AppId 可能不一致
						nonceStr: paydata.nonceStr, // 随机字符串
						package: paydata.package, // 固定值
						partnerid: '1629006880', // 微信支付商户号
						prepayid: this.id, // 统一下单订单号
						timeStamp: paydata.timeStamp, // 时间戳（单位：秒）
						paySign: paydata.paySign, // 签名，这里用的 MD5/RSA 签名
						signType: paydata.signType, // 签名算法
						success(res) {
							//跳转到用户订单
							console.log('res', res);
							uni.showToast({
								title: '充值成功',
								icon: 'success'
							});
							setTimeout(() => {
								uni.switchTab({
									url: `/pages/UserCenter/UserCenter`
								});
							}, 1500);
						},
						fail(e) {
							console.log(e);
							uni.showToast({
								title: e,
								icon: 'none'
							});
						}
					});
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'none'
					});
				}
			});
		}
	},
	onLoad() {
		this.containerHeight = _containerHeight();
		//渲染充值列表的请求
		let params = {
			token: uni.getStorageSync('usertoken')
		};
		rechargelist(params).then(res => {
			console.log(res.data.data);
			this.list = res.data.data.list;
		});
	}
};
</script>

<style lang="scss">
.recharge {
	.activecolor {
		background: rgba(255, 92, 0, 0.05) !important;
		border: 2rpx solid #ff5c00 !important;
	}
}
</style>
